package com.dayilyword.controller.msg;

import com.alibaba.fastjson.JSON;
import com.dayilyword.config.resultFormat.Result;
import com.dayilyword.config.resultFormat.ResultFormat;
import com.dayilyword.service.MsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("msg")
@Api(tags = "消息页")
public class MsgController {

    @Autowired
    MsgService msgService;

    @Autowired
    @Resource(name = "redis1")
    private StringRedisTemplate redis1;


    @PostMapping("")
    @ApiOperation("发布信息")
    public Result publishMsg(HttpServletRequest request, @RequestParam("publishMsg") String publishMsg) {
        Integer userId = (Integer) request.getSession().getServletContext().getAttribute("userId");
        msgService.publishMsg(userId, publishMsg);
        return ResultFormat.ok("publish_Msg_Is_Success");
    }

    @DeleteMapping("/{msgId}")
    @ApiOperation("删除信息")
    public Result disableMsg(@PathVariable("msgId") int msgId) {
        msgService.disableMsg(msgId);
        return ResultFormat.ok("delete_Msg_Is_OK");
    }

    @PutMapping("/{msgId}")
    @ApiOperation("修改信息")
    public Result putMsg(@PathVariable("msgId") int msgId, @RequestParam("publishMsg") String publishMsg) {
        msgService.putMsg(msgId, publishMsg);
        return ResultFormat.ok("update_Msg_Success");
    }


    @GetMapping("/getAttentionMsgList")
    @ApiOperation("获得关注人最新信息")
    public Result getAttentionMsgList(@RequestParam("firstIndex") int firstIndex,
                                      @RequestParam("lastIndex ") int lastIndex,
                                      HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getServletContext().getAttribute("userId");

        String ret = JSON.toJSONString(msgService.getAttentionMsgList(firstIndex, lastIndex, userId));
        return ResultFormat.ok(ret);
    }


    @GetMapping("/getUserHistoryMsgList")
    @ApiOperation("获得用户历史信息")
    public Result getUserHistoryMsgList(@RequestParam("firstIndex") int firstIndex,
                                        @RequestParam("lastIndex ") int lastIndex,
                                        HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getServletContext().getAttribute("userId");
        String ret = JSON.toJSONString(msgService.getUserHistoryMsgList(firstIndex, lastIndex, userId));
        return ResultFormat.ok(ret);
    }


}
